package com.gzdh.dishrecognition.util;

import java.math.BigDecimal;

/**

 * 
 * 时间： 2019-08
 * 
 * @author Administrator
 *
 */
public class KeyAdd {

	/**
	 * 键盘运算，传入当前字符串数值，再传入需要操作的数值， 操作的数值分为，0~9，小数点和删除
	 * 
	 * 限制：
	 * 
	 * 1、限制最大数值为1000元。 2、限制两位小数点。
	 * 
	 * 时间：2019-08-08
	 * 
	 * @param num 当前数值
	 * @param key 当前操作，'c'为删除一位
	 * @return
	 */
	public static String getKeyNum(String num, String key) {
		BigDecimal big1 = BigDecimal.valueOf(Double.parseDouble(num));
		BigDecimal big2 = new BigDecimal("0.0");
		BigDecimal big3 = new BigDecimal(10000);
		BigDecimal big4 = new BigDecimal(num);
		if (isNumeric(key))
			big4 = BigDecimal.valueOf(Double.parseDouble(num + key));

		if ((num.length() - num.indexOf(".") > 2 && num.contains(".") && !key.equals("c"))
				|| OperaBigDec.greaterOrEqual(big4, big3)) {
			return num;
		}
		switch (key) {
		case "1":
		case "2":
		case "3":
		case "4":
		case "5":
		case "6":
		case "7":
		case "8":
		case "9":
			if (OperaBigDec.Equal(big1, big2))
				if (num.contains("."))
					num += key;
				else
					num = key;
			else if (OperaBigDec.lessOrEqual(big1, big3))
				num += key;
			break;
		case ".":
			if (!num.contains("."))
				if (OperaBigDec.Equal(big1, big2))
					num = "0.";
				else
					num += key;
			break;
		case "0":
			if (!num.contains("."))
				if (OperaBigDec.Equal(big1, big2))
					num = "0";
				else
					num += key;
			else
				num += key;
			break;
		case "c":
			if (num.length() > 0) {
				num = num.substring(0, num.length() - 1);
				if (num.length() == 0)
					num = "0";
			} else
				num = "0";
			break;
		}
		return num;
	}

	//判断当前数值是否为数字，当前字符为数字，则返回true，其他字符则返回false
	
	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}
}
